package com.leet.code.simple_array;

/**
 * @description:
 * @author: WYG
 * @time: 2020/12/1 10:11
 */

public class SortedSquares {
    public int[] sorted(int[] nums) {
        int length = nums.length;
        int[] res = new int[length];
        res[0] = nums[0] * nums[0];
        for (int i = 1; i < length; i++) {
            nums[i] = nums[i] * nums[i];
            int temp = 0;
            for(int j = 0; j <= i; j++) {
                if (nums[i] < res[j]) {
                    temp = res[j];
                    res[j] = nums[i];
                    nums[i] = temp;
                }
                if (j == i) {
                    res[j] = nums[i];
                }
            }
        }
        return res;
    }


    public int[] sortedSquares(int[] A) {
        int n = A.length;
        int[] ans = new int[n];
        for (int i = 0, j = n - 1, pos = n - 1; i <= j;) {
            if (A[i] * A[i] > A[j] * A[j]) {
                ans[pos] = A[i] * A[i];
                ++i;
            } else {
                ans[pos] = A[j] * A[j];
                --j;
            }
            --pos;
        }
        return ans;
    }
}
